package main

import (
	"fmt"
	"net"
	"strconv"
	"sync"
	"time"
)

var ips = []string{
	"134.175.148.230",
"119.29.72.94",
"106.52.241.206",
"106.52.241.98",
"106.52.242.146",
"106.52.245.35",
"106.55.169.154",
"107.191.33.46",
"107.191.34.102",
"107.191.35.14",
"107.20.79.133",
"108.61.10.140",
"108.61.10.172",
"108.61.10.180",
"108.61.51.69",
"111.230.100.6",
"111.230.116.211",
"111.230.13.6",
"111.230.132.202",
"111.230.140.13",
"111.230.140.23",
"111.230.140.73",
"111.230.142.140",
"111.230.145.88",
"111.230.147.63",
"111.230.151.88",
"111.230.166.113",
"111.230.200.118",
"111.230.204.29",
"111.230.204.97",
"111.230.221.216",
"111.230.238.166",
"111.230.238.198",
"111.231.231.34",
"111.231.246.66",
"114.203.209.23",
"114.203.209.24",
"114.203.209.25",
"114.203.209.26",
"114.203.209.51",
"118.126.107.29",
"118.126.88.140",
"118.193.85.58",
"118.193.85.59",
"118.193.85.60",
"118.89.19.180",
"118.89.32.148",
"118.89.35.49",
"118.89.36.157",
"118.89.42.237",
"118.89.51.206",
"118.89.62.52",
"118.89.64.204",
"119.28.133.68",
"119.28.182.202",
"119.29.0.228",
"119.29.1.158",
"119.29.104.155",
"119.29.105.122",
"119.29.108.93",
"119.29.11.67",
"119.29.12.58",
"119.29.120.23",
"119.29.121.248",
"119.29.135.121",
"119.29.137.157",
"119.29.143.20",
"119.29.147.114",
"119.29.153.43",
"119.29.155.232",
"119.29.155.91",
"119.29.157.214",
"119.29.158.105",
"119.29.158.48",
"119.29.158.82",
"119.29.170.250",
"119.29.171.115",
"119.29.172.249",
"119.29.173.63",
"119.29.174.188",
"119.29.174.48",
"119.29.18.107",
"119.29.190.90",
"119.29.191.176",
"119.29.196.112",
"119.29.205.169",
"119.29.208.247",
"119.29.218.116",
"119.29.221.40",
"119.29.229.165",
"119.29.231.237",
"119.29.26.230",
"119.29.27.113",
"119.29.3.208",
"119.29.33.248",
"119.29.4.237",
"119.29.40.24",
"119.29.5.167",
"119.29.5.57",
"119.29.5.88",
"119.29.54.121",
"119.29.57.14",
"119.29.57.193",
"119.29.58.223",
"119.29.60.139",
"119.29.64.185",
"119.29.65.149",
"119.29.65.42",
"119.29.69.244",
"119.29.78.119",
"119.29.78.250",
"119.29.80.194",
"119.29.81.82",
"119.29.85.36",
"119.29.86.110",
"119.29.88.129",
"119.29.92.59",
"123.207.111.76",
"123.207.116.41",
"123.207.15.80",
"123.207.18.94",
"123.207.234.146",
"123.207.26.27",
"123.207.27.198",
"123.207.29.181",
"123.207.42.218",
"123.207.87.103",
"123.207.89.123",
"123.207.93.60",
"128.1.89.64",
"128.1.89.65",
"128.1.89.66",
"128.1.89.67",
"129.204.14.59",
"129.204.163.133",
"129.204.163.140",
"129.204.19.50",
"129.204.203.250",
"129.204.21.122",
"134.175.143.25",
"134.175.160.138",
"134.175.178.195",
"134.175.180.74",
"134.175.216.174",
"134.175.224.198",
"139.199.0.189",
"139.199.10.238",
"139.199.156.144",
"139.199.157.237",
"139.199.158.129",
"139.199.16.122",
"139.199.169.212",
"139.199.176.53",
"139.199.192.45",
"139.199.193.114",
"139.199.197.148",
"139.199.206.84",
"139.199.3.216",
"139.199.68.114",
"139.199.68.89",
"149.129.100.99",
"149.129.103.24",
"149.129.104.207",
"149.129.105.125",
"149.129.107.133",
"149.129.108.226",
"149.129.111.192",
"149.129.66.135",
"149.129.68.112",
"149.129.68.244",
"149.129.69.76",
"149.129.71.182",
"149.129.74.143",
"149.129.75.113",
"149.129.75.214",
"149.129.85.45",
"149.129.86.181",
"149.129.87.118",
"149.129.98.170",
"162.62.14.178",
"162.62.15.129",
"162.62.20.196",
"162.62.21.7",
"162.62.26.90",
"162.62.27.113",
"162.62.27.160",
"162.62.33.38",
"18.156.124.179",
"18.156.22.85",
"18.157.114.84",
"18.157.174.207",
"18.194.128.34",
"18.194.146.103",
"18.194.189.6",
"18.194.213.120",
"18.194.64.9",
"18.194.65.18",
"18.195.93.144",
"18.196.171.8",
"18.210.223.88",
"18.233.240.7",
"18.235.10.175",
"18.235.11.114",
"182.254.175.19",
"182.254.176.126",
"182.254.188.11",
"182.254.193.72",
"182.254.206.249",
"182.254.233.199",
"193.112.12.167",
"193.112.120.105",
"193.112.126.36",
"193.112.129.140",
"193.112.137.83",
"193.112.144.40",
"193.112.151.63",
"193.112.153.62",
"193.112.157.135",
"193.112.161.100",
"193.112.161.32",
"193.112.165.163",
"193.112.168.157",
"193.112.168.192",
"193.112.168.50",
"193.112.170.32",
"193.112.172.33",
"193.112.175.141",
"193.112.175.88",
"193.112.177.23",
"193.112.177.44",
"193.112.177.49",
"193.112.181.245",
"193.112.183.125",
"193.112.192.173",
"193.112.195.19",
"193.112.197.247",
"193.112.199.197",
"193.112.204.180",
"193.112.208.175",
"193.112.213.241",
"193.112.216.245",
"193.112.217.240",
"193.112.217.247",
"193.112.218.107",
"193.112.220.203",
"193.112.220.26",
"193.112.221.51",
"193.112.223.139",
"193.112.241.77",
"193.112.243.55",
"193.112.246.170",
"193.112.246.212",
"193.112.246.27",
"193.112.251.194",
"203.195.130.57",
"203.195.136.236",
"203.195.137.22",
"203.195.148.67",
"203.195.161.240",
"203.195.166.243",
"203.195.192.97",
"203.195.195.43",
"203.195.199.203",
"203.195.199.33",
"203.195.213.71",
"203.195.230.93",
"203.195.238.171",
"203.75.1.137",
"203.75.1.142",
"203.75.1.145",
"203.75.1.167",
"203.75.1.178",
"210.242.234.22",
"210.242.234.24",
"210.242.234.31",
"216.155.155.165",
"3.120.99.69",
"3.124.97.17",
"3.127.31.211",
"3.214.222.90",
"3.223.61.220",
"35.156.21.107",
"35.157.117.246",
"35.158.11.220",
"35.158.15.113",
"35.171.108.5",
"47.240.1.164",
"47.240.1.29",
"47.240.104.166",
"47.240.109.237",
"47.240.13.94",
"47.240.23.185",
"47.240.23.223",
"47.240.23.224",
"47.240.23.30",
"47.240.23.33",
"47.240.23.7",
"47.240.23.86",
"47.240.24.103",
"47.240.24.110",
"47.240.24.124",
"47.240.4.157",
"47.240.6.8",
"47.244.108.228",
"47.244.175.112",
"47.244.30.243",
"47.52.112.97",
"47.52.247.226",
"47.52.41.154",
"47.74.131.245",
"47.74.132.220",
"47.74.133.104",
"47.74.134.227",
"47.74.135.67",
"47.74.144.82",
"47.74.145.50",
"47.74.145.62",
"47.75.137.149",
"47.75.198.14",
"47.75.91.123",
"47.88.155.155",
"47.88.157.227",
"47.88.157.234",
"47.88.158.90",
"47.88.172.39",
"47.88.225.175",
"47.88.237.164",
"47.90.59.51",
"47.91.159.76",
"47.91.247.135",
"50.16.99.149",
"52.2.191.202",
"52.21.249.79",
"52.22.11.173",
"52.54.137.235",
"52.58.73.130",
"52.68.63.92",
"52.69.73.201",
"52.72.207.249",
"54.146.144.99",
"54.146.83.213",
"54.217.231.62",
"54.228.187.223",
"54.228.197.57",
"54.247.105.169",
"54.247.105.64",
"54.247.122.64",
"54.65.236.157",
"54.74.88.110",
"54.82.141.67",
"54.92.53.85",
}

var portss = []int{
	20, 
	21, 
	22, 
	23, 
	25, 
	43, 
	53, 
	67, 
	68, 
	80, 
	110,
	113,
	143,
	161,
	194,
	443,
	587,
	631,
	666,
	873,
	1500,
	3306,
	3389,
	5044,
	5601,
	6379,
	8080,
	9000,
	9200,
	11211,
	19022,
	27017,
}


var ports = []int{
	9000,
	6379,
}

//端口扫描
func main() {
	maxRoutins := 50
	jobChan := make(chan int, maxRoutins)
	resultChan := make(chan map[string]string, maxRoutins)
	var job sync.WaitGroup
	go func() {
		for _, ip := range ips {
			go func(ip string){
				for _, port :=range ports{
					jobChan <- 1
					job.Add(1)
					go scanPort(ip, port, resultChan, &job)
				}
			}(ip)
		}
	}()

	
	go func(){
		time.Sleep(1*time.Second)
		job.Wait()
		close(resultChan)
	}()

	for data := range resultChan {
		<-jobChan

		if data["ok"]=="1"{
			fmt.Println(data["ip"], data["port"], data["res"])
		}
	}

	close(jobChan)
}

func scanPort(ip string, port int, resultChan chan map[string]string, job *sync.WaitGroup) {
	res := map[string]string{
		"ip":   ip,
		"port": strconv.Itoa(port),
	}

	//fmt.Println("start conn on port:" + strconv.Itoa(port))
	conn, err := net.DialTimeout("tcp", ip+":"+strconv.Itoa(port), time.Second*2)
	if err != nil {
		res["res"] = err.Error()
		res["ok"] = "0"
	} else if conn != nil {
		res["res"] = "conn success"
		res["ok"] = "1"
	} else {
		res["res"] = "conn refause"
		res["ok"] = "0"
	}

	resultChan <- res
	job.Done()
}